package com.everwin.web.utils;

import com.alibaba.druid.util.StringUtils;
import com.everwin.common.constant.SignConstants;
import com.everwin.common.exception.CheckSignException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Date;

public class AESUtils {

    private static final String KEY_AES = "AES";

    /**
     * AES加密默认盐值
     */
    public static final String AES_KEY = "sM7!tsv?5ygRo;h.";

    public static String encrypt(String src, String key) {
        if (StringUtils.isEmpty(key)) {
            key = AES_KEY;
        }
        try {
            byte[] raw = key.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_AES);
            Cipher cipher = Cipher.getInstance(KEY_AES);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(src.getBytes());
            return byte2hex(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return src;
    }

    public static String decrypt(String src, String key) {
        if (StringUtils.isEmpty(key)) {
            key = AES_KEY;
        }
        try {
            byte[] raw = key.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_AES);
            Cipher cipher = Cipher.getInstance(KEY_AES);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = hex2byte(src);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return src;
    }

    public static byte[] hex2byte(String strhex) {
        if (null == strhex) {
            return null;
        }
        int l = strhex.length();
        if (l % 2 == 1) {
            return null;
        }
        byte[] b = new byte[l / 2];
        for (int i = 0; i != l / 2; i++) {
            b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
                    16);
        }
        return b;
    }

    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }
    public static boolean checkSign(String sign,long sendTime) {
        long time = new Date().getTime();
        if(time-sendTime> SignConstants.SIGN_DURANTION){
            return false;
        }
        String message=decrypt(sign,null);
        String[] checkInfo = message.split(";");
        if(checkInfo.length!=3){
            return false;
        }
        String username=checkInfo[0];
        String password=checkInfo[1];
        String decSendTimeStr=checkInfo[2];
        if(Long.parseLong(decSendTimeStr)!=sendTime){
            return false;
        }
        UsernamePasswordToken token=new UsernamePasswordToken(username,password,true);
        Subject subject = SecurityUtils.getSubject();
        try{
            subject.login(token);
        }catch(AuthenticationException e){
            throw new CheckSignException("签名中账号密码有误");
        }
        return true;
    }

    public static void main(String[] args) {
        long time = new Date().getTime();
        System.out.println(time);
        String msg="yiyi;123456;"+time;
        String encrypt = encrypt(msg, null);
        System.out.println(encrypt);
    }
}
